package com.esfak47.mediafetcher.plugins.javafxapp;

import com.esfak47.common.logger.Logger;
import com.esfak47.common.logger.LoggerFactory;
import com.esfak47.mediafetcher.core.event.Event;
import com.esfak47.mediafetcher.core.event.EventGroupEnum;
import com.esfak47.mediafetcher.core.event.EventHandler;
import com.esfak47.mediafetcher.core.event.EventReceiver;

import java.util.HashSet;
import java.util.Set;

/**
 * @author tony
 * @date 2018/5/5
 */
public class JavaFXEventHandler implements EventHandler {
    private Logger logger = LoggerFactory.getLogger(JavaFXEventHandler.class);

    private final Set<EventGroupEnum> eventGroupEnums;
    private final Set<String> supportSubjects;

    private EventReceiver eventReceiver;

    public JavaFXEventHandler() {
        this.eventGroupEnums = new HashSet<>();
        this.eventGroupEnums.add(EventGroupEnum.APP);
        this.supportSubjects = new HashSet<>();
        this.supportSubjects.add("ready");
    }

    @Override
    public Set<EventGroupEnum> getSupportGroup() {
        return eventGroupEnums;
    }

    @Override
    public Set<String> supportSubject() {
        return supportSubjects;
    }

    @Override
    public void onEvent(Event event) {
        switch (event.getSubject()) {
            case "ready":
                WebViewApplication.setOnEnd(aVoid -> eventReceiver.send((new Event(EventGroupEnum.SYSTEM, "end", null))));
                WebViewApplication.main(null);
                break;
            default:
                break;
        }
    }

    public void setEventReceiver(EventReceiver eventReceiver) {
        logger.info("setting eventReceiver");
        this.eventReceiver = eventReceiver;
    }
}
